Skip to main content

Activity: Use chat variables #

Chat variables are a powerful way to interact with Copilot. They act like tools to carry out specific tasks.

Scenario: Let's keep working on our sample project

If you haven't done it already, clone the following repo:

git clone https://github.com/softchris/flask-sample.git

-1- Ask about a specific file

Let's ask Copilot about a specific file. For example, let's ask about the app.py file:

tell me about #codebase api.py

You should see an output like this:

```text
The api.py file in the #codebase is a Flask application that provides CRUD operations for managing products and carts. Here's a breakdown:

Key Features:
Static Data:

products: A list of dictionaries representing product details (e.g., id, name, price, stock).
carts: A list of dictionaries representing shopping carts (e.g., id, items).
Products API:

GET /products: Returns a list of all products.
GET /products/<int:product_id>: Returns details of a specific product by id. Returns a 404 error if not found.
POST /products: Creates a new product. The id is auto-incremented.
PUT /products/<int:product_id>: Updates an existing product by id. Returns a 404 error if not found.
DELETE /products/<int:product_id>: Deletes a product by id.
Carts API:

GET /carts: Returns a list of all carts.
GET /carts/<int:cart_id>: Returns details of a specific cart by id. Returns a 404 error if not found.
POST /carts: Creates a new cart with an auto-incremented id and an empty items list.
PUT /carts/<int:cart_id>: Updates an existing cart by id. Returns a 404 error if not found.
DELETE /carts/<int:cart_id>: Deletes a cart by id.
Flask App Configuration:

The app runs in debug mode for development purposes.
Example Usage:
To fetch all products: GET /products
To add a new product: POST /products with JSON payload like {"name": "Tablet", "price": 300, "stock": 15}.
This file is the core of the project, providing the API endpoints for interacting with the products and carts data. Let me know if you'd like to explore or modify any specific part!
```

-2- Do a change in the workspace

Let's say you've done a change in the workspace and you want to know what files have changed so you know what to commit. You can use the following prompt:

#changes

If you've added to the products list, you should see an output like this:

The api.py file has been updated with additional static data for products. Specifically, the following products were added:

Tablet
Headphones
Smartwatch
Charger
Mouse
Keyboard
Monitor
Printer
Let me know if you need further assistance!

-3- Ask about a specific file

If you're interested in a specific file, like if it has this or that feature, or if you just want to know what it does, you can use the following prompt:

#file: LICENSE

You should see an output like this:

The LICENSE file contains the MIT License, which is a permissive open-source license. It allows anyone to use, copy, modify, merge, publish, distribute, sublicense, and sell copies of the software, as long as the original copyright notice and permission notice are included in all copies or substantial portions of the software.

The license also states that the software is provided "as is," without any warranty of any kind, and the authors are not liable for any claims, damages, or other liabilities arising from its use.

-4- Bringing in extra context

Imagine a situation where you need code to be generated based on a specific context. Your company for example, might have placed a style guide at a specific URL and you would like it so that Copilot can look at that URL and generate code based on that.

Or maybe you saw an interesting article but you don't have time to read it but still want to know the most important parts of it. You can use the #fetch variable to fetch the content of a URL and use it as context for Copilot. For example:

what are some practices to avoid according to #fetch https://docs.python-guide.org/writing/style/

It will now ask for your permission before fetching the content of the URL. If you allow it, it will fetch the content and use it as context. Here's a sample output:

- **Using "Magic" Code**: Avoid overly complex or "magical" constructs that reduce readability and make debugging difficult. Explicit and straightforward code is preferred.

- **Ignoring Conventions for Private Properties**: Avoid directly accessing or modifying properties and methods that are intended to be private. Instead, follow the convention of prefixing private properties with an underscore (_).

- **Multiple Statements on One Line**: Avoid writing multiple disjointed statements on the same line. This reduces readability and clarity.

- **Modifying Lists While Iterating**: Avoid removing or modifying items in a list while iterating through it. This can lead to unexpected behavior.

- **Reusing Mutable Objects**: Avoid modifying a list or object that is referenced by multiple variables. This can lead to unintended side effects.

- **Complex Function Exit Points**: Avoid having multiple main exit points in a function. This can make debugging and refactoring more difficult.

- **Overusing Advanced Features**: Avoid using advanced Python features (like changing object creation or embedding C routines) unless absolutely necessary. These can make the code harder to understand and maintain.

- **Dense or Nested Code**: Avoid writing dense or deeply nested code. Flat and sparse code is easier to read and maintain.

By adhering to these practices, you can write more readable, maintainable, and Pythonic code.